package com.realife.eson.common.util.jwt;


import com.realife.eson.common.asserts.AssertNP;

/**
 * @version V1.0
 * @author: guangcheng
 * @date: 2022/4/14 16:13
 * @description:
 */
public class TokenJWTServer implements ITokenServer {
    //private static final Logger log = LoggerFactory.getLogger(TokenJWTServer.class);
    private TokenConfiguration tokenConfiguration = new TokenConfiguration();

    public TokenJWTServer() {
    }

    public String generateToken(ITokenInfo info) throws Exception {
        return JWTHelper.generateToken(info, this.tokenConfiguration.getUserPriKey(), this.calExpireMinutes(info.getExpiration()));
    }

    public ITokenInfo getInfoFromTokenByJwt(String token) throws Exception {
        return this.getInfoFromToken(token);
    }

    public ITokenInfo getInfoFromToken(String token) throws Exception {
        if (AssertNP.legalParams(token)) {
            return JWTHelper.getInfoFromToken(token, this.tokenConfiguration.getUserPubKey());
        }
        return null;
    }

    public ICoreJWTInfo getInfoFromTokenCore(String token) throws Exception {
        if (AssertNP.legalParams(token)) {
            return JWTHelper.getInfoFromTokenCore(token, this.tokenConfiguration.getUserPubKey());
        }
        return null;
    }

    public String generateTokenCore(ICoreJWTInfo info) throws Exception {
        return JWTHelper.generateTokenCore(info, this.tokenConfiguration.getUserPriKey(), 31536000);
    }
}

